<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        clearable
        placeholder="路由名称"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
      />
      <el-input
        v-model="listQuery.title"
        clearable
        placeholder="菜单名称"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
      />
      <el-button
        v-waves
        style="margin-left: 10px"
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-circle-plus-outline"
        @click="handleAdd(0)"
      >
        添加
      </el-button>
      <el-button
        class="filter-item"
        type="danger"
        icon="el-icon-delete"
        @click="handleDelete(0)"
      >
        批量删除
      </el-button>
    </div>

    <el-table
      ref="itemDataTable"
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      row-key="id"
      border
      fit
      highlight-current-row
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      style="width: 100%;margin-top: 20px"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column label="菜单名称" align="left">
        <template slot-scope="{ row: { titleWrapper }}">
          <span v-html="titleWrapper" />
        </template>
      </el-table-column>
      <el-table-column label="路由名称" align="center">
        <template slot-scope="{ row: { nameWrapper }}">
          <span v-html="nameWrapper" />
        </template>
      </el-table-column>
      <el-table-column
        label="路由地址"
        prop="path"
        align="center"
      />
      <el-table-column
        label="文件位置"
        prop="url"
        align="center"
      />
      <el-table-column
        label="重定向地址"
        prop="redirect"
        align="center"
      />
      <el-table-column label="图标" align="center" width="70">
        <template slot-scope="{ row: { icon }}">
          <svg-icon v-if="icon !== ''" :icon-class="icon" />
        </template>
      </el-table-column>
      <el-table-column label="菜单类型" align="center" width="100">
        <template slot-scope="{row: { is_link }}">
          <el-tag v-if="is_link === 1" size="mini" type="success">链接</el-tag>
          <el-tag v-else size="mini">菜单</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="公用" width="100" align="center">
        <template slot-scope="{ row }">
          <it-switch
            v-model="row.public_menu"
            :active-value="1"
            :inactive-value="0"
            active-text="打开"
            inactive-text="关闭"
            @change="handleMenuStatus($event, row.id, 'public_menu')"
          />
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100" align="center">
        <template slot-scope="{ row }">
          <it-switch
            v-model="row.status"
            :active-value="1"
            :inactive-value="0"
            active-text="打开"
            inactive-text="关闭"
            @change="handleMenuStatus($event, row.id, 'status')"
          />
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" width="120">
        <template slot-scope="{ row }">
          <el-input-number
            v-model="row.sort"
            controls-position="right"
            style="width: 100%"
            @change="handleMenuSort($event, row.id)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="270" align="center" fixed="right">
        <template slot-scope="{ row: { id } }">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="handleEdit(id)">编辑</el-button>
          <el-button size="mini" type="warning" icon="el-icon-circle-plus-outline" @click="handleAdd(id)">添加</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑弹出框 -->
    <el-dialog
      :title="detailEditID !==0 ? '编辑角色菜单' : '添加角色菜单'"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      @close="closeDialog"
    >
      <auth-menu-detail :id="detailEditID" ref="authMenuDetail" :pid="detailEditPid" @ended="handleSubmitEnd" />
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves'
import TnInnerTextSwitch from '@/components/TNInnerTextSwitch'
import AuthMenuDetail from '../../components/auth/menu/Detail'
import { wrapperKeyword } from '@/utils'
import { getTableTreeData, updateAuthMenu, deleteAuthMenu } from '@/api/auth-menu'

export default {
  name: 'AuthMenu',
  components: { 'it-switch': TnInnerTextSwitch, AuthMenuDetail },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      listLoading: false,
      total: 0,
      listQuery: {
        title: '',
        name: ''
      },
      list: [],
      detailEditID: 0,
      detailEditPid: 0,
      dialogFormVisible: false
    }
  },
  watch: {
    list(val) {
      this._wrapperKeyword(val)
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    handleFilter() {
      this.getList()
    },
    handleAdd(id) {
      this.detailEditID = 0
      this.detailEditPid = id
      this.dialogFormVisible = true
      this.$nextTick().then(() => {
        this.$refs.authMenuDetail.getAllMenuNode()
      })
    },
    handleEdit(id) {
      this.detailEditID = id
      this.detailEditPid = 0
      this.dialogFormVisible = true
      this.$nextTick().then(() => {
        this.$refs.authMenuDetail.getAllMenuNode()
      })
    },
    handleDelete(id) {
      this.$confirm('此操作将永久删除该数据，是否继续', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const ids = []
        // 判断是否为批量删除
        if (id === 0) {
          const _selection = this.$refs.itemDataTable.selection
          // console.log(_selection)
          if (_selection.length === 0) {
            this.$message({
              message: '请选择需要删除的数据',
              type: 'error'
            })
            return
          }
          _selection.forEach((value, index) => {
            ids.push(value.id)
          })
        } else {
          ids.push(id)
        }

        // console.log(ids)

        deleteAuthMenu(ids).then(response => {
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.getList()
        })
      }).catch(() => {})
    },
    handleMenuSort(value, id) {
      // console.log(id, value)
      updateAuthMenu({
        id,
        field: 'sort',
        value
      }).then(response => {
        this.$message({
          message: response.msg,
          type: 'success',
          duration: 2000
        })
      })
    },
    handleMenuStatus(value, id, field) {
      updateAuthMenu({
        id,
        field,
        value
      }).then(response => {
        this.$message({
          message: response.msg,
          type: 'success',
          duration: 2000
        })
      })
    },
    closeDialog() {
      this.$refs.authMenuDetail.toDefault()
      this.detailEditID = 0
      this.detailEditPid = 0
    },
    handleSubmitEnd() {
      this.dialogFormVisible = false
      this.getList()
    },
    getList() {
      this.listLoading = true
      getTableTreeData(this.listQuery).then(response => {
        const { data } = response
        this.list = data
        this.listLoading = false
      })
    },
    _wrapperKeyword(val) {
      val.forEach(menu => {
        menu.titleWrapper = wrapperKeyword(this.listQuery['title'] || '', menu.title)
        menu.nameWrapper = wrapperKeyword(this.listQuery['name'] || '', menu.name)
        if (menu.children && menu.children.length > 0) {
          this._wrapperKeyword(menu.children)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-input-number.is-controls-right >>> span {
  width: 22px;
}
</style>
